<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad">
      <van-cell v-for="item in list"
                :key="item.com_id.toString()">
        <van-image slot="icon"
                   round
                   width="30"
                   height="30"
                   style="margin-right: 10px;"
                   :src="item.aut_photo" />
        <span style="color: #466b9d;"
              slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">
              {{ item.pubdate | date }}
            </span>
            <van-button size="mini"
                        type="default"
                        @click="showReply(item)">
              回复({{ item.reply_count }})
            </van-button>
          </p>
        </div>
        <van-icon slot="right-icon"
                  :name="item.is_liking ? 'like' : 'like-o'"
                  @click="zan(item)" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable
                 placeholder="请输入评论内容"
                 v-model.trim="content">
        <van-button slot="button"
                    size="mini"
                    type="info"
                    @click="add">
          发布
        </van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-action-sheet v-model="isShowReply"
                      title="评论回复"
                      style="height: 60%">
      <CommentReply v-if="isShowReply"
                    :comment="comment"
                    @addCount="addCount"></CommentReply>
    </van-action-sheet>
  </div>
</template>

<script>
import CommentReply from './CommentReply'
import {
  addComment,
  addCommentZan,
  deleteCommentZan,
  getCommentList
} from '@/api/article'
export default {
  name: 'ArticleComment',
  props: {},
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '', // 评论的内容
      isShowReply: false,
      comment: {}
    }
  },
  components: {
    CommentReply
  },
  computed: {
    articleId() {
      return this.$route.params.id
    }
  },
  methods: {
    async onLoad() {
      const res = await getCommentList(this.articleId, this.offset)
      // console.log(res)
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      if (res.data.results.length === 0) {
        this.finished = true
      }
      // 修改offset
      this.offset = res.data.last_id
    },
    async add() {
      // console.log(this.content)
      if (!this.content) return this.$toast('请输入评论内容')
      const res = await addComment(this.articleId, this.content)
      // 添加成功
      // console.log(res)
      this.$toast.success('添加评论成功')
      // 清空内容
      this.content = ''
      // 不用强制更新 改掉is_liking 状态
      res.data.new_obj.is_liking = false
      // 添加
      this.list.unshift(res.data.new_obj)
    },
    async zan(item) {
      if (item.is_liking) {
        await deleteCommentZan(item.com_id)
      } else {
        await addCommentZan(item.com_id)
      }
      this.$toast.success('操作成功')
      item.is_liking = !item.is_liking
      // 强制更新
      // this.$forceUpdate()
    },
    showReply(item) {
      this.comment = item
      this.isShowReply = true
    },
    addCount() {
      this.comment.reply_count++
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
